Fabian Gülzau
30 November 2017
labsgeom_annotateggsaveTufte (1997: 92-105) schlägt vor, dass Grafiken sich unter anderem anhand ihrer “data-ink ratio” beurteilen lassen. Die “data ink-ratio” bestimmt das Verhältnis zwischen “Farbe”, die Daten darstellt und derjenigen, welche ersetzt werden kann ohne das es zu einem Informationsverlust kommt:
Data-ink ration = data-ink / total ink used to print the graphic
Anzahl der vergebenen Nobelpreise über den Zeitverlauf:
nobel.year <- nobel %>%
filter(!is.na(year)) %>%
group_by(year) %>%
summarise(prizes = n())Der ‘simple’ Plot:
ggplot(data = nobel.year, mapping = aes(x = year, y = prizes)) +
geom_bar(stat = "identity")Unter Berücksichtigung der ‘data-ink ratio’:
ggplot(data = nobel.year, mapping = aes(x = year, y = prizes)) +
geom_bar(width = 0.1, stat = "identity") + # Breite der Balken
theme(
axis.ticks.length = unit(x = 0.25, units = "cm"),
panel.background = element_blank()
)Einige Ergebnisse der Übung:
Einige Ergebnisse der Übung:
Einige Ergebnisse der Übung:
Einige Ergebnisse der Übung:
Die Aufgabe berührt einen Streit zwischen Verfechtern von Klarheit und einem eher spielerischen Ansatz, der schon seit den 1990er Jahren tobt:
Exemplarisch für den eher nüchternen Ansatz ist Edward Tufte:
Während auf der anderen Seite etwa Nigel Holmes stand. Alberto Cairo (2013) sieht aber auch Otto Neurath als einen Vorgänger (s. auch Gerd Arntz):
Empirische Studien zum Thema (vgl. Cairo 2013: 66-68):
“[n]ot a matter of functionality, but visual style” (Cairo 2013: 66).
Die Gestaltung von Grafiken kann über den Befehl themes erfolgen. In ggplot2 stehen zehn ‘themes’ zur Verfügung. Darunter theme_minimal, theme_classic bis hin zum theme_void.
ggplot(data = nobel.year, mapping = aes(x = year, y = prizes)) +
geom_bar(width = 0.1, stat = "identity") +
theme_classic() # Verwendung von themesDarüberhinaus gibt es noch ein zusätzliches Paket, welches weitere ‘themes’ enthält: ggthemes.
Hier findet sich u.a. anderem auch ein ‘theme’, welches von Tufte inspiriert ist: theme_tufte.
ggplot(data = nobel.year, mapping = aes(x = year, y = prizes)) +
geom_bar(width = 0.1, stat = "identity") +
theme_tufte()Des Weiteren bietet ggthemes auch einige ‘geoms’, die auf Tuftes Arbeiten basieren:
geom_rangeframegeom_tufteboxplot# Age of the Nobel laureate
nobel.age <- nobel %>%
filter(gender != "org" & !is.na(year) & !is.na(born) & category != "") %>%
mutate(born = strtoi(str_extract_all(born, "[:digit:]{4}"))) %>%
filter(born > 0) %>%
mutate(yearprize = year - born)
# Boxplot (Note also example for the use of statistical transformation)
ggplot(data = nobel.age, mapping = aes(x = gender, y = yearprize)) +
geom_tufteboxplot() +
theme_tufte()Es gibt aber viele weitere Gestaltungsoptionen, die durch ggthemes eröffnet werden:
Unter anderem auch das theme_stata:
ggplot(data = nobel.age, mapping = aes(x = gender, y = yearprize)) +
geom_boxplot() +
facet_wrap(~ category) +
theme_stata()Oder auch theme_economist:
ggplot(data = nobel.age, mapping = aes(x = gender, y = yearprize)) +
geom_boxplot() +
facet_wrap(~ category) +
theme_economist()On January 28, 1986, the space shuttle Challenger exploded and seven austronauts died because two rubber O-rings leaked. These rings had lost their resiliency because the shuttle was launched on a very cold day (Tufte 1997: 39).
Am Tag vor dem Start von Space Shuttle Challenger wurden 13 Folien erstellt, welche die NASA-Manager vom Start der Rakete abhalten sollten.
“The charts were unconvincing” (Tufte 1997: 30):
Die Daten sind auf Moodle verfügbar (readRDS) und bereits von Fahrenheit in Celsius umgerechnet.
Versucht die Grafik von Tufte zu reproduzieren.
Wenn die basale Grafik steht, versucht sie mit folgenden Befehlen zu ergänzen:
geom_annotatelabstheme oder ggthemesspace.plot <- ggplot() +
geom_point(data = space, mapping = aes(x = Temperature, y = Damage)) +
scale_x_continuous(limits = c(-5, 30), breaks = seq(from = -5, to = 30, by = 5)) +
annotate(geom = "rect", xmin = -2.78, xmax = -1.67, ymin = -Inf, ymax = Inf,
alpha = 0.2) +
annotate(geom = "text", label = "-2.78° to -1.67° range of \n temperature forecast of \n space shuttle Challenger", x = 3.5, y = 1.5) +
annotate(geom = "text", label = "SRM-25 Challenger: \n Fatal damage", x = 2.1, y = 12) +
labs(x = "Temperature (°C) of field joints at time of launch",
y = "O-ring damage index, each launch",
title = "History of O-Ring Damage",
subtitle = "Relationship between Temperature and O-Ring Damage",
caption = "Data available at: http://vincentarelbundock.github.io/Rdatasets/datasets.html") +
theme_classic()
space.plotEin weiteres Beispiel von Tufte (1997) ist eine Karte, die John Snow verwendete, um die Ursache einer Cholera-Epidemie zu finden.
Wir benötigen die Dateien map, death und pump aus dem Moodlekurs sowie das Paket ggmap:
Mit Hilfe des Befehls lässt sich die heutige Lage der Choleraepidemie plotten:
ggmap(map)Der Befehl stellt einen “layer” dar und lässt sich mit Hilfe der Dateien death und pump um weitere Ebenen erweitern.
map <- readRDS(file = "C:\\Users\\User\\HU-Box\\Seafile\\Meine Bibliothek\\Seminare\\WS 2017\\Sessions\\Session 6\\Map")
death <- readRDS(file = "C:\\Users\\User\\HU-Box\\Seafile\\Meine Bibliothek\\Seminare\\WS 2017\\Sessions\\Session 6\\Death")
pump <- readRDS(file = "C:\\Users\\User\\HU-Box\\Seafile\\Meine Bibliothek\\Seminare\\WS 2017\\Sessions\\Session 6\\Pump")
ggmap(map) +
geom_point(data = death, aes(x = lon, y = lat, size = count)) +
geom_point(data = pump, aes(x = lon, y = lat), color = "red", size = 8)Wir werden und mit den Möglichkeiten von ggmap in späteren Sitzungen noch auseinandersetzen.
“Numbers become evidence by being in relation to” (Tufte 1997: 44)
# Create random deviates from observed data
n <- list(100, 100)
mean <- list(mean(space$Temperature), mean(space$Damage, na.rm = TRUE))
sd <- list(sd(space$Temperature), sd(space$Damage, na.rm = TRUE))
arguments <- list(n = n, mean = mean, sd = sd)
add.data <- pmap(.l = arguments, rnorm)
space.add <- data.frame(number = seq(1, 125, 1),
Temperature = c(space$Temperature, add.data[[1]]),
Damage = c(space$Damage, abs(add.data[[2]])))
space.add %>%
head(n = 40)## number Temperature Damage
## 1 1 18.88889 0.0000000
## 2 2 21.11111 4.0000000
## 3 3 20.55556 0.0000000
## 4 4 26.66667 NA
## 5 5 20.00000 0.0000000
## 6 6 19.44444 0.0000000
## 7 7 22.22222 0.0000000
## 8 8 22.77778 0.0000000
## 9 9 21.11111 0.0000000
## 10 10 13.88889 4.0000000
## 11 11 17.22222 2.0000000
## 12 12 21.11111 4.0000000
## 13 13 25.55556 0.0000000
## 14 14 19.44444 0.0000000
## 15 15 11.66667 11.0000000
## 16 16 19.44444 0.0000000
## 17 17 23.88889 0.0000000
## 18 18 21.11111 0.0000000
## 19 19 27.22222 0.0000000
## 20 20 24.44444 0.0000000
## 21 21 26.11111 0.0000000
## 22 22 23.88889 4.0000000
## 23 23 14.44444 4.0000000
## 24 24 24.44444 4.0000000
## 25 25 -2.25000 12.0000000
## 26 26 25.10544 3.8813427
## 27 27 21.66033 4.1637197
## 28 28 27.11795 0.2011934
## 29 29 16.21135 3.9787441
## 30 30 29.34218 1.7264579
## 31 31 19.63338 3.1838900
## 32 32 11.64141 6.7779613
## 33 33 14.02835 1.4393072
## 34 34 22.68731 1.6271249
## 35 35 20.88427 2.2711001
## 36 36 13.81967 2.5480427
## 37 37 22.95301 2.0770790
## 38 38 15.70290 9.0834599
## 39 39 27.01530 7.0502367
## 40 40 24.47450 6.5002929
space.plot <- ggplot(data = space.add, mapping = aes(x = Temperature, y = Damage)) +
geom_point() +
geom_smooth(method = "loess") +
scale_x_continuous(limits = c(-5, 30), breaks = seq(from = -5, to = 30, by = 5)) +
annotate(geom = "rect", xmin = -2.78, xmax = -1.67, ymin = -Inf, ymax = Inf,
alpha = 0.2) +
annotate(geom = "text", label = "-2.78° to -1.67° range of \n temperature forecast of \n space shuttle Challenger", x = 3.5, y = 1.5) +
annotate(geom = "text", label = "SRM-25 Challenger: \n Fatal damage", x = 2.1, y = 12) +
labs(x = "Temperature (°C) of field joints at time of launch",
y = "O-ring damage index, each launch",
title = "History of O-Ring Damage",
subtitle = "Relationship between Temperature and O-Ring Damage",
caption = "Data available at: http://vincentarelbundock.github.io/Rdatasets/datasets.html") +
theme_classic()
space.plotOft werden Grafiken nicht in RMarkdown-Dokumente eingebettet, sondern in Word und PowerPoint. Hierfür müssen die Grafiken zunächst in einem entsprechenden Format gespeichert werden. Hierfür können wir die Funktion ggsave verwenden, die Teil von ggplot2 ist.
So könnten wir unsere Grafik, welche im Objekt space.plot in R gesichert ist, folgendermaßen sichern:
# TIFF-Format
ggsave(filename = "SpacePlot", plot = space.plot, device = "tiff", path = ".//", dpi = 600)
# PDF-Format
ggsave(filename = "SpacePlot", plot = space.plot, device = "pdf", path = ".//", dpi = 600)